package edu.neumont.App;

import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import edu.neumont.entities.Entities;
import edu.utility.xuggle.MyAudio;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class BrowseActivity extends ListActivity{
	PrintWriter writer;
	View.OnClickListener listener;
	Scanner sc;
	ArrayAdapter<String> files;
	ListView list;
	String path;
	Thread t;
	Handler h;
	AudioTrack audioTrack;
	ObjectInputStream ois;
	int sampleCount = 0;
	private ArrayList<String> movieTypes = new ArrayList<String>();
	private ArrayList<String> audioTypes = new ArrayList<String>();
	private ArrayList<String> pictureTypes = new ArrayList<String>();
	private final String root = "C:";
	private boolean audioStreaming;
	private String nowPlaying = "";
	private TextView nowPlayingView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try{
			writer = new PrintWriter(Entities.network.getSocket().getOutputStream());
			sc = new Scanner(Entities.network.getSocket().getInputStream());
		}catch(Exception ex){}
		audioStreaming = false;
		populateLists();		
		list = getListView();
		list.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
				String text = ((TextView)arg1).getText().toString();
				
				if(audioStreaming){
					writer.println("stopAudio");
					writer.flush();
					audioStreaming = false;
					try{
						Thread.sleep(1000);
						while(Entities.network.getSocket().getInputStream().available() > 0)
							ois.readObject();
						
					}catch(Exception ex){}
				}
				if(nowPlaying.equals(text)){
					nowPlaying = "";
					((TextView)arg1).setTextColor(Color.LTGRAY);
					nowPlayingView = null;
				}
				else if(isAudioType(text)){
					if(nowPlayingView != null)
						nowPlayingView.setTextColor(Color.LTGRAY);
					((TextView)arg1).setTextColor(Color.rgb(255, 165, 0));
					nowPlaying = text;
					nowPlayingView = (TextView)arg1;
					playSong(text);
				}
				else if(isPictureType(text))
					viewPicture(text);
				else if(isMovieType(text))
					playVideo(text);
				else if(text.equals("<--" + path))
					loadFiles(path.substring(0, path.lastIndexOf("\\")));
				else
					loadFiles(text);
			}
		});
		loadFiles(root);
	}
	private void populateLists(){
		movieTypes.add(".avi");movieTypes.add(".mp4");
		audioTypes.add(".mp3");
		pictureTypes.add(".png");pictureTypes.add(".gif");pictureTypes.add(".jpg");
	}
	 public void playVideo(String fileName){;
		writer.println(fileName);
		writer.flush();
		startActivity(new Intent(BrowseActivity.this, MovieActivity.class));
	 }
	 public void playSong(String fileName){
		writer.println(fileName);
		writer.flush();
		audioStreaming = true;
		
		h = new Handler();
		t = new Thread(){
			public void run(){
				audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT, 20000, AudioTrack.MODE_STREAM);
				audioTrack.play();
				try{
				ois = new ObjectInputStream(Entities.network.getSocket().getInputStream());
				while(audioStreaming){
					MyAudio audio = (MyAudio)ois.readObject();
					audioTrack.write(audio.getBytes(), 0, audio.getBytes().length);
					
				}
				}catch(Exception ex){};
				audioTrack.release();			
			}
		};	
		t.start();
	 }
	 public void viewPicture(String fileName){
		writer.println(fileName);
		writer.flush();
		startActivity(new Intent(BrowseActivity.this, PictureActivity.class));
	 }
	 private void loadFiles(String message){
		list.setAdapter(null);
		files = new ArrayAdapter<String>(BrowseActivity.this, R.layout.browse);
		try{
			writer = new PrintWriter(Entities.network.getSocket().getOutputStream());
			writer.println(message);
			writer.flush();	
		}catch(Exception ex){}
		
		String line = sc.nextLine();
		path = line;
		files.add("<--" + path);
		line = sc.nextLine();
		while(!line.equals("donesendingfiles")){
			files.add(line);
			line = sc.nextLine();
		}
		list.setAdapter(files);
	}
		public boolean isMovieType(String name){
			if(name.contains("."))
				return movieTypes.contains(name.substring(name.lastIndexOf(".")).toLowerCase());
			return false;
		}
		public boolean isAudioType(String name){
			if(name.contains("."))
				return audioTypes.contains(name.substring(name.lastIndexOf(".")).toLowerCase());
			return false;
		}
		public boolean isPictureType(String name){
			if(name.contains("."))
				return pictureTypes.contains(name.substring(name.lastIndexOf(".")).toLowerCase());
			return false;
		}
}
